package com.xywl.escort.view.bandcard

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.trello.rxlifecycle2.LifecycleTransformer
import com.xywl.escort.R
import com.xywl.escort.adapter.BandCardListAdapter
import com.xywl.escort.base.BaseActivity
import com.xywl.escort.bean.BandCardListResp
import com.xywl.escort.bean.BaseResp
import com.xywl.escort.common.PreferenceKeys.USER_ID
import com.xywl.escort.utils.RxSPTool
import com.xywl.escort.utils.ToastUtil
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.activity_band_card_list.*
import kotlinx.android.synthetic.main.layout_empty_view.view.*
import timber.log.Timber
import java.util.*
import javax.inject.Inject

@Route(path = "/xy/band/card/list")
class BandCardListActivity : BaseActivity(), BandCardListContract.View {

    @Inject
    lateinit var mPresenter: BandCardListContract.Presenter
    private lateinit var mAdapter: BandCardListAdapter
    private val mList = ArrayList<BandCardListResp.DataBean.ListBean>()

    override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_band_card_list)

        initView()
        addListener()
    }

    private fun initView() {
        showBackButton()
        setTitleText("银行卡关联")

        mAdapter = BandCardListAdapter(R.layout.item_band_card_list, mList)
        recycler_band_card.layoutManager = LinearLayoutManager(this)
        recycler_band_card.adapter = mAdapter

        val emptyView = LayoutInflater.from(this).inflate(R.layout.layout_empty_view, null)
        val tvEmpty = emptyView.tv_empty
        tvEmpty.text = "暂无银行卡"
        mAdapter.emptyView = emptyView
    }

    private fun addListener() {
        btn_add_band_card.setOnClickListener {
            val userId = RxSPTool.getString(this, USER_ID)
            mPresenter.isSetPayPwd(userId)
        }

        mAdapter.setOnItemClickListener { _, _, position ->
            val listBean = mList[position]
            ARouter.getInstance().build("/xy/band/card/detail")
                    .withObject("bandBean", listBean)
                    .navigation()
        }
    }

    override fun onResume() {
        super.onResume()
        mPresenter.takeView(this)
        mPresenter.getBandCardList()
    }

    override fun isActive(): Boolean {
        return false
    }

    override fun <T : Any?> bindToLife(): LifecycleTransformer<T> {
        return this.bindToLifecycle()
    }

    override fun hasSetPayPwd() {
        ARouter.getInstance().build("/xy/verify/pay/pwd")
                .withString("path", "/xy/band/card/bind")
                .navigation()
    }

    override fun noSetPayPwd() {
        ARouter.getInstance().build("/xy/pwd/setting/pay").navigation()
    }

    override fun findIsFitPayPwdFailure(msg: String?) {
        ToastUtil.showTip(this, msg)
    }

    override fun getBandCardListSuccess(dataBean: BandCardListResp.DataBean?) {
        Timber.e("getBandCardListSuccess")
        progress_bar.visibility = View.GONE
        recycler_band_card.visibility = View.VISIBLE
        mList.clear()
        if (dataBean!!.list.size > 0) {
            mList.add(dataBean.list[0])
            btn_add_band_card.visibility = View.GONE
        } else {
            btn_add_band_card.visibility = View.VISIBLE
        }
        mAdapter.setNewData(mList)
    }

    override fun getBandCardListFailure() {
        Timber.e("getBandCardListFailure")
        progress_bar.visibility = View.GONE
        recycler_band_card.visibility = View.VISIBLE
        btn_add_band_card.visibility = View.VISIBLE
    }

    override fun updateUserBankCardDefStatusSuccess(resp: BaseResp?) {
        ToastUtil.showTip(this, "设置成功")
        mPresenter.getBandCardList()
    }

    override fun updateUserBankCardDefStatusFailure(msg: String?) {
        ToastUtil.showTip(this, msg)
    }
}
